/**
 * Functions to generate Git summaries
 */
const vscode = require('vscode');
const { getCommits, analyzeCommits } = require('../utils/gitUtils');
const { createSummaryPanel } = require('../ui/summaryPanel');

/**
 * Generates a summary of all Git commits in the current repository
 */
async function generateSummary() {
  const workspaceFolders = vscode.workspace.workspaceFolders;
  if (!workspaceFolders || workspaceFolders.length === 0) {
    vscode.window.showErrorMessage("No workspace folder is open. Please open a Git repository.");
    return;
  }

  // Show progress indicator
  return vscode.window.withProgress({
    location: vscode.ProgressLocation.Notification,
    title: "Generating Git summary...",
    cancellable: false
  }, async (progress) => {
    try {
      progress.report({ increment: 10, message: "Fetching commits..." });
      const commits = await getCommits();
      
      progress.report({ increment: 40, message: "Analyzing commits..." });
      const summary = await analyzeCommits(commits);
      
      progress.report({ increment: 40, message: "Preparing results..." });
      await createSummaryPanel(summary);
      
      progress.report({ increment: 10, message: "Done!" });
      return summary;
    } catch (error) {
      vscode.window.showErrorMessage(`Error generating summary: ${error.message}`);
      throw error;
    }
  });
}

/**
 * Generates a summary of Git commits within a specified date range
 */
async function generateSummaryWithDateRange() {
  try {
    // Ask user for start date
    const startDateString = await vscode.window.showInputBox({
      prompt: "Enter start date (YYYY-MM-DD)",
      placeHolder: "e.g., 2023-01-01"
    });
    
    if (!startDateString) return; // User cancelled
    
    // Ask user for end date
    const endDateString = await vscode.window.showInputBox({
      prompt: "Enter end date (YYYY-MM-DD)",
      placeHolder: "e.g., 2023-12-31"
    });
    
    if (!endDateString) return; // User cancelled
    
    // Validate dates
    const startDate = new Date(startDateString);
    const endDate = new Date(endDateString);
    
    if (isNaN(startDate.getTime()) || isNaN(endDate.getTime())) {
      vscode.window.showErrorMessage("Invalid date format. Please use YYYY-MM-DD.");
      return;
    }
    
    // Generate the summary with date range
    return vscode.window.withProgress({
      location: vscode.ProgressLocation.Notification,
      title: "Generating Git summary...",
      cancellable: false
    }, async (progress) => {
      progress.report({ increment: 10, message: "Fetching commits..." });
      const commits = await getCommits(startDate, endDate);
      
      progress.report({ increment: 40, message: "Analyzing commits..." });
      const summary = await analyzeCommits(commits);
      
      progress.report({ increment: 40, message: "Preparing results..." });
      await createSummaryPanel(summary, { startDate, endDate });
      
      progress.report({ increment: 10, message: "Done!" });
      return summary;
    });
    
  } catch (error) {
    vscode.window.showErrorMessage(`Error generating summary: ${error.message}`);
    throw error;
  }
}

module.exports = {
  generateSummary,
  generateSummaryWithDateRange
};
